package edu.hm.risikomanagement;

import edu.hm.risikomanagement.library.DatabaseData;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class Berechnungen extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.berechnungen);

		if (DatabaseData.isNetworkAvailable(getApplicationContext())) {
			updateTableData();
		} else {
			Intent i = new Intent(Berechnungen.this, Error.class);
			i.putExtra("error", this.getString(R.string.connection_error));
			startActivityForResult(i, 1);
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_CANCELED && requestCode == 1) {
			finish();
		}
	}

	private void updateTableData() {

		TableLayout table = (TableLayout) findViewById(R.id.tbl_Berechnungen_Liste);

		table.removeAllViews();

		TableRow row = new TableRow(this);
		row.setPadding(0, 0, 0, 25);
		TextView t = new TextView(this);
		SpannableString spannableString = new SpannableString("Name");
		spannableString.setSpan(new UnderlineSpan(), 0,
				spannableString.length(), 0);
		t.setText(spannableString);
		row.addView(t);
		t = new TextView(this);
		spannableString = new SpannableString("Abs. Kosten");
		spannableString.setSpan(new UnderlineSpan(), 0,
				spannableString.length(), 0);
		t.setText(spannableString);
		row.addView(t);
		t = new TextView(this);
		spannableString = new SpannableString("Bewertung");
		spannableString.setSpan(new UnderlineSpan(), 0,
				spannableString.length(), 0);
		t.setText(spannableString);
		t.setTypeface(null, Typeface.BOLD);
		row.addView(t);
		t = new TextView(this);
		spannableString = new SpannableString("Wahrsch.");
		spannableString.setSpan(new UnderlineSpan(), 0,
				spannableString.length(), 0);
		t.setText(spannableString);
		row.addView(t);
		table.addView(row);

		String selection = DatabaseData
				.getDataFromDatabase("risikoBerechnungen");
		String[] items = selection.split("@@@");

		int rowCounter = 0;
		for (String item : items) {

			String[] elements = item.split("&&&");
			row = new TableRow(this);
			row.setPadding(0, 7, 0, 7);
			if (rowCounter % 2 == 0) {
				row.setBackgroundColor(Color.rgb(50, 109, 162));
			}

			int counter = 0;
			for (final String element : elements) {

				if (counter == 0) {
					/*
					 * row.setOnClickListener(new OnClickListener() { public
					 * void onClick(View v) { Intent i = new
					 * Intent(Berechnungen.this, BenutzerDetails.class);
					 * i.putExtra("id", element); startActivity(i); } });
					 */
				} else if ((counter == 2) || (counter == 3)) {
					t = new TextView(this);
					t.setText(element + " Euro   ");
					if (counter == 3) {
						t.setTypeface(null, Typeface.BOLD);
					}
					row.addView(t);
				} else if (counter == 4) {
					t = new TextView(this);
					t.setText(element + " %");
					t.setGravity(5);
					row.addView(t);
				} else {
					t = new TextView(this);
					t.setText(element + "    ");
					t.setWidth(200);
					row.addView(t);
				}

				counter++;

			}

			table.addView(row);
			rowCounter++;

		}

	}

}